﻿using System;
using System.Globalization;
using System.IO;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using JTLeigh.Tools.Development.CopySourceAsHtml.Properties;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JTLeigh.Tools.Development.CopySourceAsHtml.RegressionTests
{

    /// <summary>
    /// Regression tests.
    /// </summary>
    [TestClass]
    public class RegressionTestFixture
    {
        #region Methods

        #region Scenarios

        /// <summary>
        /// Ensures all regression test scenarios pass.
        /// </summary>
        [TestMethod]
        public void TestScenarios()
        {
            bool result;

            result = true;
            using (StreamWriter writer = new StreamWriter("TestScenariosResults.html"))
            {
                foreach (string fileName in Directory.GetFiles(".", "*.scenario"))
                {
                    result = TestScenario(writer, fileName) && result;
                }
                writer.Close();
            }
            Assert.IsTrue(result, "TestScenarios failed. See TestScenariosResults.html for detailed results.");
        }

        /// <summary>
        /// Ensures the specified regression test scenario passes.
        /// </summary>
        /// <param name="summaryWriter">The <see cref="StreamWriter"/> to write the results of the 
        /// regression test to.</param>
        /// <param name="fileName">The path and filename of the regression test file.</param>
        private static bool TestScenario(StreamWriter summaryWriter, string fileName)
        {
            XmlSerializer serializer;
            Scenario scenario;
            Copier copier;
            string html;
            bool result;
			SettingsPropertyValue propertyValue;

            serializer = new XmlSerializer(typeof(Scenario));
            using (var reader = new StreamReader(fileName))
            {
                scenario = (Scenario)serializer.Deserialize(reader);
                reader.Close();
            }

            copier = new Copier(scenario.Settings, scenario.PlainTextStyle, scenario.LineNumberStyle);
            html = copier.CopyAsHtml(scenario.Path, scenario.Rtf);

            using (var writer = new StreamWriter(fileName + ".html"))
            {
                writer.Write(html);
                writer.Close();
            }

            result = scenario.Html.Equals(html);

            // write header
            if (result)
            {
                summaryWriter.WriteLine("<table width=\"100%\" style=\"border-width: 1px; border-collapse: collapse; color: white; background: green; font-family: Arial; font-size: 10pt; font-weight: bold;\">");
            }
            else
            {
                summaryWriter.WriteLine("<table width=\"100%\" style=\"border-width: 1px; border-collapse: collapse; color: black; background: red; font-family: Arial; font-size: 10pt; font-weight: bold;\">");
            }
            summaryWriter.WriteLine("<tr><td width=\"200\">Filename:</td><td>{0}</td></tr>", fileName);
            summaryWriter.WriteLine("<tr><td>Description:</td><td>{0}</td></tr>", scenario.Description);
            summaryWriter.WriteLine("<tr><td>Contributed By:</td><td>{0}</td></tr>", scenario.ContributedBy);
            summaryWriter.Write("<tr><td>Settings:</td><td>");
            foreach (SettingsProperty property in scenario.Settings.Properties)
            {
                propertyValue = scenario.Settings.PropertyValues[property.Name];
				if ((propertyValue != null) && (propertyValue.SerializedValue != null) && (!propertyValue.SerializedValue.Equals(property.DefaultValue)))
                {
                    summaryWriter.Write("{0}=&quot;{1};&quot; ", property.Name, HtmlTools.HtmlEncode(propertyValue.SerializedValue.ToString()));
                }
            }
            summaryWriter.WriteLine("</td></tr>");
            summaryWriter.WriteLine("<tr><td>Expected:</td></tr>");
            summaryWriter.WriteLine("</table>");

            // write expected result
            summaryWriter.WriteLine("<table width=\"100%\"\"><tr><td>");
            summaryWriter.WriteLine("<!-- EXPECTED RESULT EXPECTED RESULT EXPECTED RESULT --->");
            summaryWriter.Write(scenario.Html);
            summaryWriter.WriteLine("<!-- EXPECTED RESULT EXPECTED RESULT EXPECTED RESULT --->");
            summaryWriter.WriteLine("</td></tr></table>");

            // write header
            if (result)
            {
                summaryWriter.WriteLine("<table width=\"100%\" style=\"border-width: 1px; border-collapse: collapse; color: white; background: green; font-family: Arial; font-size: 10pt; font-weight: bold;\">");
            }
            else
            {
                summaryWriter.WriteLine("<table width=\"100%\" style=\"border-width: 1px; border-collapse: collapse; color: black; background: red; font-family: Arial; font-size: 10pt; font-weight: bold;\">");
            }
            summaryWriter.WriteLine("<tr><td>Actual:</td></tr>");
            summaryWriter.WriteLine("</table>");

            // write actual result
            summaryWriter.WriteLine("<table width=\"100%\"\"><tr><td>");
            summaryWriter.WriteLine("<!-- ACTUAL RESULT ACTUAL RESULT ACTUAL RESULT --->");
            summaryWriter.Write(html);
            summaryWriter.WriteLine("<!-- ACTUAL RESULT ACTUAL RESULT ACTUAL RESULT --->");
            summaryWriter.WriteLine("</td></tr></table>");

            // write separator
            summaryWriter.WriteLine("<br/>");

            return result;
        }

        #endregion // Scenarios

        #endregion // Methods
    }

}
